<template>
  <div class="create">
    <h2>{{ $route.params.id ? "编辑" : "创建" }}简历</h2>
    <el-form ref="form" :model="form" label-width="80px">
      <el-tabs type="border-card">
        <el-tab-pane label="基本信息">
          <el-form-item label="名称">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="称号">
            <el-input v-model="form.title"></el-input>
          </el-form-item>
          <el-form-item label="类型">
            <el-select v-model="form.categories" multiple placeholder="请选择">
              <el-option
                v-for="item in categoriesList"
                :key="item._id"
                :label="item.name"
                :value="item._id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="评级">
            <el-rate
              style="margin-top: 10px"
              :max="9"
              show-score
              v-model="form.scores.difficult"
            ></el-rate>
          </el-form-item>
          <el-form-item label="头像">
            <el-upload
              class="avatar-uploader"
              :action="uploadUrl"
              :headers='getAuthHeaders()'
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="form.avatar" :src="form.avatar" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-tab-pane>
        <el-tab-pane label="技能信息">
          <el-button size="small" @click="form.skills.push({})"
            ><i class="el-icon-plus"></i>添加技能</el-button
          >
          <el-row type="flex" style="flex-wrap: wrap">
            <el-col :span="12" v-for="(item, idx) in form.skills" :key="idx">
              <el-form-item label="名称">
                <el-input v-model="item.name"></el-input>
              </el-form-item>
              <!-- <el-form-item label="图标">
                <el-upload
                  class="avatar-uploader"
                  :action="$http.defaults.baseURL + '/upload'"
                  :show-file-list="false"
                  :on-success="res => $set(item,'icon',res.url)"
                >
                  <img v-if="item.icon" :src="item.icon" class="avatar" />
                  <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
              </el-form-item> -->
              <el-form-item label="描述">
                <el-input type="textarea" v-model="item.description"></el-input>
              </el-form-item>
              <!-- <el-form-item label="描述">
                <el-input type="textarea" v-model="item.description"></el-input>
              </el-form-item> -->
              <el-form-item>
                <el-button type='danger' size='small' @click="form.skills.splice(i,1)">删除</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>
      <el-form-item></el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">{{
          $route.params.id ? "修改保存" : "立即创建"
        }}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        id: "",
        name: "",
        avatar: null,
        title: "",
        categories: [],
        scores: {
          difficult: 0,
          skills: 0,
          attack: 0,
          survive: 0,
        },
        skills: [],
        item1: "",
        item2: "",
        usageTips: "",
        battleTips: "",
        teamTips: "",
        partners: [],
      },
      categoriesList: [],
    };
  },
  methods: {
    async getCategories() {
      const res = await this.$http.get("common/categories");
      this.categoriesList = res.data.filter(i => i.parent != null);
    },
    async onSubmit() {
      console.log("submit!");
      let url = this.form.id == "" ? "common/heroes" : "common/heroes/edit";
      const res = await this.$http.post(url, this.form);
      console.log(res);

      this.$message({
        type: "success",
        message: this.form.id == "" ? "创建成功!" : "修改成功",
      });

      this.form = {
        id: "",
        name: "",
        parent: null,
      };
      this.getParent();
    },
    async getItem(id) {
      const res = await this.$http.get(`common/heroes/${id}`);
      // this.form = Object.assign({}, this.form, res.data);

      this.form = res.data;
    },
    handleAvatarSuccess(res) {
      this.form.avatar = res.url;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
  created() {
    this.$route.params.id && this.getItem(this.$route.params.id);
  },
  mounted() {
    this.getCategories();
  },
};
</script>